package com.pzhu.user.dao;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.pzhu.base.common.utils.mybatisflex.LogicDeleteService;
import com.pzhu.user.domain.entity.Menu;
import com.pzhu.user.domain.ro.MenuSearchRO;
import com.pzhu.base.common.utils.mybatisflex.QueryUtils;

import java.util.List;

import static com.pzhu.user.domain.entity.table.MenuTableDef.MENU;

/**
 * @author User
 * @description 针对表【menu(菜单表)】的数据库操作Mapper
 * @createDate 2024-02-20 18:15:20
 * @Entity com.pzhu.auth.domain.entity.Menu
 */
public interface MenuDAO extends LogicDeleteService<Menu> {

    default Page<Menu> page(Page<Menu> page, MenuSearchRO menuSearchRo) {
        QueryCondition condition = QueryUtils.and(
                QueryUtils.del(MENU.DELETED_ID),
                MENU.MENU_NAME.like(menuSearchRo.getMenuName()),
                MENU.MENU_CODE.eq(menuSearchRo.getMenuCode()));
        return page(page, condition);
    }

    default Page<Menu> pageFrom(Long startId, MenuSearchRO menuSearchRo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        QueryUtils.and(
                QueryUtils.del(MENU.DELETED_ID),
                MENU.MENU_NAME.like(menuSearchRo.getMenuName()),
                MENU.MENU_CODE.eq(menuSearchRo.getMenuCode()),
                MENU.ID.gt(startId));
        queryWrapper.limit(menuSearchRo.getSize());
        long count = count(queryWrapper);
        Page<Menu> menuPage = new Page<>();
        if (count == 0) {
            return menuPage;
        }
        List<Menu> menus = list(queryWrapper);
        menuPage.setRecords(menus);
        queryWrapper.orderBy(MENU.ID.desc());
        menuPage.setTotalRow(count);
        return menuPage;
    }

    default Page<Menu> search(MenuSearchRO menuSearchRo) {
        QueryWrapper wrapper = new QueryWrapper();
        QueryUtils.and(
                QueryUtils.or(
                        MENU.MENU_CODE.like(menuSearchRo.getMenuCode()),
                        MENU.MENU_NAME.like(menuSearchRo.getMenuName())),
                QueryUtils.del(MENU.DELETED_ID));
        return page(menuSearchRo, wrapper);
    }
}
